package com.chuangke.metadata.controller;

import com.chuangke.common.http.HttpResult;
import com.chuangke.metadata.entity.SysSchemaColumn;
import com.chuangke.metadata.entity.SysSchemaTable;
import com.chuangke.metadata.service.SysSchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("schema")
public class SysSchemaController {

	@Autowired
	private SysSchemaService sysSchemaService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody SysSchemaTable table) {
		sysSchemaService.save(table);
		return HttpResult.ok() ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/delete/{tableId}")
	public HttpResult delete(@PathVariable("tableId") String tableId) {
		sysSchemaService.delete(tableId);
		return HttpResult.ok() ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/tables/{moudleId}")
	public HttpResult findTables(@PathVariable("moudleId") String moudleId) {
		return HttpResult.ok(sysSchemaService.findTables(moudleId)) ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/load/{tableId}")
	public HttpResult loadTable(@PathVariable("tableId") String tableId) {
		return HttpResult.ok(sysSchemaService.loadTable(tableId)) ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/saveDetail")
	public HttpResult saveDetail(@RequestBody SysSchemaColumn sysSchemaColumn) {
		sysSchemaService.saveDetail(sysSchemaColumn);
		return HttpResult.ok() ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/delDetailByIds")
	public HttpResult delDetailByIds(@RequestBody List<String> ids) {
		sysSchemaService.deleteDetail(ids);
		return HttpResult.ok();
	}

}
